package class03;

import java.util.*;

/**
 * <a href="https://leetcode.cn/problems/min-stack/">155. 最小栈</a>
 */
public class Code05_GetMinStack {
    // 常数时间返回栈中最小元素的数据结构
    public static class MinStack {
        public Stack<Integer> stack, minStack;

        public MinStack() {
            this.stack = new Stack<>();
            this.minStack = new Stack<>();
        }

        public void push(int value) {
            stack.push(value);
            if (minStack.isEmpty()) {
                minStack.push(value);
            } else {
                minStack.push(Math.min(value, minStack.peek()));
            }
        }

        public void pop() {
            minStack.pop();
            stack.pop();
        }

        public int top() {
            return stack.peek();
        }

        public int getMin() {
            return minStack.peek();
        }
    }
}
